package cn.edu.scau.cmi.ema.controller.rest;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.io.IOException;

import com.google.gson.Gson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.service.base.CmiPsonService;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;

import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.WebDataBinder;

@Controller("CrawlerfilecolumnRestController")
public class CrawlerfilecolumnRestController extends CmiEmaController {

	/**
	 * Create a new Crawlerfilecolumn entity
	 * 
	 */
	@RequestMapping(value = "/newCrawlerfilecolumn", method = RequestMethod.POST)
	@ResponseBody
	public Object newCrawlerfilecolumn(@RequestBody Crawlerfilecolumn crawlerfilecolumn) {
		crawlerfilecolumnService.saveCrawlerfilecolumn(crawlerfilecolumn);
		return JSONObject.parse(cmiPsonService
				.toJsonString(crawlerfilecolumnDAO.findCrawlerfilecolumnByPrimaryKey(crawlerfilecolumn.getId()), 2));
	}

	@RequestMapping(value = "/restNewCrawlerfilecolumn", method = RequestMethod.POST)
	@ResponseBody
	public Object restNewCrawlerfilecolumn(@RequestBody String crawlerfilecolumnJson) {
		Crawlerfilecolumn crawlerfilecolumn = new Gson().fromJson(crawlerfilecolumnJson, Crawlerfilecolumn.class);
		appendReferences(crawlerfilecolumn);
		crawlerfilecolumnService.saveCrawlerfilecolumn(crawlerfilecolumn);
		return JSONObject.parse(cmiPsonService
				.toJsonString(crawlerfilecolumnDAO.findCrawlerfilecolumnByPrimaryKey(crawlerfilecolumn.getId()), 2));
	}

	@RequestMapping(value = "/saveCrawlerfilecolumn", method = RequestMethod.PUT)
	@ResponseBody
	public Crawlerfilecolumn saveCrawlerfilecolumn(@RequestBody Crawlerfilecolumn crawlerfilecolumn) {
		crawlerfilecolumnService.saveCrawlerfilecolumn(crawlerfilecolumn);
		return crawlerfilecolumnDAO.findCrawlerfilecolumnByPrimaryKey(crawlerfilecolumn.getId());
	}

	@RequestMapping(value = "/restSaveCrawlerfilecolumn", method = RequestMethod.PUT)
	@ResponseBody
	public Object restSaveCrawlerfilecolumn(@RequestBody String crawlerfilecolumnJson) {
		Crawlerfilecolumn crawlerfilecolumn = new Gson().fromJson(crawlerfilecolumnJson, Crawlerfilecolumn.class);
		appendReferences(crawlerfilecolumn);
		CmiPsonService cmiPsonService = new CmiPsonService();
		crawlerfilecolumnService.saveCrawlerfilecolumn(crawlerfilecolumn);
		return JSONObject.parse(cmiPsonService
				.toJsonString(crawlerfilecolumnDAO.findCrawlerfilecolumnByPrimaryKey(crawlerfilecolumn.getId()), 1));
	}

	@RequestMapping(value = "/restDeleteCrawlerfilecolumn/{crawlerfilecolumnId}", method = RequestMethod.DELETE)
	@ResponseBody
	public void restDeleteCrawlerfilecolumn(@PathVariable Integer crawlerfilecolumnId) {
		Crawlerfilecolumn crawlerfilecolumn = crawlerfilecolumnDAO
				.findCrawlerfilecolumnByPrimaryKey(crawlerfilecolumnId);
		crawlerfilecolumnService.deleteCrawlerfilecolumn(crawlerfilecolumn);
	}

	@RequestMapping(value = "/restConfirmDeleteCrawlerfilecolumns")
	@ResponseBody
	public Object restConfirmDeleteCrawlerfilecolumns(@RequestBody Set<Integer> deleteCrawlerfilecolumnsIds) {
		Set<Crawlerfilecolumn> deleteCrawlerfilecolumns = crawlerfilecolumnDAO
				.findCrawlerfilecolumnsByIds(deleteCrawlerfilecolumnsIds);
		Set<Crawlerfilecolumn> referencedCrawlerfilecolumns = crawlerfilecolumnService
				.loadReferenceCrawlerfilecolumns(deleteCrawlerfilecolumns);
		deleteCrawlerfilecolumns.removeAll(referencedCrawlerfilecolumns);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("referencedEntitys", referencedCrawlerfilecolumns);
		jsonObject.put("deleteEntitys", deleteCrawlerfilecolumns);
		return JSONObject.parse(jsonObject.toJSONString());
	}

	@RequestMapping("/restDeleteCrawlerfilecolumns")
	@ResponseBody
	public Object restDeleteCrawlerfilecolumns(@RequestBody Set<Crawlerfilecolumn> deleteCrawlerfilecolumns) {
		Iterator<Crawlerfilecolumn> iterator = deleteCrawlerfilecolumns.iterator();
		while (iterator.hasNext()) {
			Crawlerfilecolumn currentCrawlerfilecolumn = iterator.next();
			crawlerfilecolumnService.deleteCrawlerfilecolumn(currentCrawlerfilecolumn);
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("url", "listCrawlerfilecolumns");
		return JSONObject.parse(jsonObject.toJSONString());
	}
	// loadself

	@RequestMapping(value = "/restLoadCrawlerfilecolumn/{crawlerfilecolumnId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadCrawlerfilecolumn(@PathVariable Integer crawlerfilecolumnId,
			@RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService
				.toPsonString(crawlerfilecolumnDAO.findCrawlerfilecolumnByPrimaryKey(crawlerfilecolumnId), 2, cmiPagination));
	}
	// loadReferenceds
	// TODO loadReferencedsPson，这个方法有一些问题，待修复

	@RequestMapping(value = "/restLoadCrawlerfilecolumnCrawlerfilestandardcolumns/{crawlerfilecolumnId}/crawlerfilestandardcolumns/{relatedcrawlerfilestandardcolumnId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadCrawlerfilecolumnCrawlerfilestandardcolumns(@PathVariable Integer crawlerfilecolumnId,
			@PathVariable Integer relatedcrawlerfilestandardcolumnId, @RequestBody CmiPagination cmiPagination) {
		Crawlerfilestandardcolumn crawlerfilestandardcolumn = crawlerfilestandardcolumnDAO
				.findCrawlerfilestandardcolumnByPrimaryKey(relatedcrawlerfilestandardcolumnId, -1, -1);
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(crawlerfilestandardcolumn, 2, cmiPagination));
	}

	public void appendReferences(Crawlerfilecolumn crawlerfilecolumn) {

		// 引用表是纯联系表的情况
		Set<Crawlerfilestandardcolumn> crawlerfilestandardcolumnsForCrawlerfilecolumn = new HashSet<>();
		for (Crawlerfilestandardcolumn crawlerfilestandardcolumnForCrawlerfilecolumn : crawlerfilecolumn
				.getCrawlerfilestandardcolumnsForCrawlerfilecolumn()) {
			crawlerfilestandardcolumnsForCrawlerfilecolumn.add(crawlerfilestandardcolumnService
					.findCrawlerfilestandardcolumnByPrimaryKey(crawlerfilestandardcolumnForCrawlerfilecolumn.getId()));
		}
		crawlerfilecolumn
				.setCrawlerfilestandardcolumnsForCrawlerfilecolumn(crawlerfilestandardcolumnsForCrawlerfilecolumn);
	}

	@RequestMapping(value = "/restGetCrawlerfilecolumnCrawlerfilestandardcolumns/{crawlerfilecolumnId}/crawlerfilestandardcolumns", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetCrawlerfilecolumnCrawlerfilestandardcolumns(@PathVariable Integer crawlerfilecolumnId,
			@RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject
				.parse(cmiPsonService.toPsonString(crawlerfilecolumnDAO.findCrawlerfilecolumnByPrimaryKey(crawlerfilecolumnId)
						.getCrawlerfilestandardcolumnsForCrawlerfilecolumn(), 2, cmiPagination));
	}

	@RequestMapping(value = "/restListCrawlerfilecolumns", method = RequestMethod.GET)
	@ResponseBody
	public Object restListCrawlerfilecolumns() {
		return JSONObject.parse(cmiPsonService.toJsonString(crawlerfilecolumnService.loadCrawlerfilecolumns(), 2));
	}

	@RequestMapping(value = "/restListCrawlerfilecolumnsPost", method = RequestMethod.POST)
	@ResponseBody
	public Object restListCrawlerfilecolumnsPost(@RequestBody CmiPagination cmiPagination) {

		CmiPsonService cmiPsonService = new CmiPsonService();
		cmiPagination = crawlerfilecolumnPaginationService.updateAppPagination(cmiPagination);
		int startResult = (cmiPagination.getCurrentPagination() - 1) * cmiPagination.getPaginationSize();

		Set<Crawlerfilecolumn> crawlerfilecolumns = crawlerfilecolumnDAO.findAllCrawlerfilecolumns(startResult,
				cmiPagination.getPaginationSize());
		String crawlerfilecolumnString = cmiPsonService.toPsonString(crawlerfilecolumns, 2, cmiPagination);

		return JSONObject.parse(crawlerfilecolumnString);
	}

	@RequestMapping("/crawlerfilecolumnSelectBox")
	@ResponseBody
	public JSONObject crawlerfilecolumnSelectBox(@RequestParam(required = false) String searchTable,
			@RequestParam(required = false) Set<Integer> searchValue, // 杨宏宇2019-9-10：searchValue是在selectpage.js的616行定义，如果要修改searchValue的名称，1.可以修改在selectpage.js修改；2.可以
																		// @RequestParam(value="searchValue", required =
																		// false) Set<Integer> someName
			@RequestParam(required = false, defaultValue = "10") int paginationSize,
			@RequestParam(required = false, defaultValue = "1") int pageNumber,
			@RequestParam(value = "qWord[]", required = false) String queryword,
			@RequestParam(required = false) String selectItem) throws IOException {
		Set entitySet = null;
		Integer count = 0;

		// 初始化原始值

		if (searchValue != null) {
			switch (searchTable) {
			// 外键

			// 被引用键

			case "Crawlerfilestandardcolumn":
				entitySet = crawlerfilestandardcolumnService.loadCrawlerfilestandardcolumnsByIdSet(searchValue);
				break;
			}
			// 没有测试，深度需要多少？？？
			String jsonData = cmiPsonService.toJsonString(entitySet, 1);
			JSONArray jsonArray = new JSONArray();
			jsonArray = (JSONArray) JSON.parse((String) jsonData);

			JSONObject jsonObject = new JSONObject();
			jsonObject.put("list", jsonArray);
			jsonObject.put("totalRow", entitySet.size());
			return jsonObject;
		}

		// 不根据关键字进行搜索
		if (queryword == null || queryword.trim() == "") {
			switch (selectItem) {
			// 外键

			// 被引用键

			case "Crawlerfilestandardcolumn":
				entitySet = crawlerfilestandardcolumnDAO
						.findAllCrawlerfilestandardcolumns((pageNumber - 1) * paginationSize, paginationSize);
				count = crawlerfilestandardcolumnService.countCrawlerfilestandardcolumns();
				break;
			}
		} else {
			switch (selectItem) {
			// 外键

			// 被引用键

			case "Crawlerfilestandardcolumn":
				entitySet = crawlerfilestandardcolumnDAO.findCrawlerfilestandardcolumnByNameContaining(queryword,
						(pageNumber - 1) * paginationSize, paginationSize);
				count = crawlerfilestandardcolumnService.countCrawlerfilestandardcolumnsByName(queryword);
				break;
			}
		}
		/**
		 * 将Json数据转换成jsonArray形式，需要通过pson方式进行转换，然后再转换成JsonArray。因为存在懒加载
		 * 没有直接使用CmiPsonService中的fromJson方法是因为，fromJson方法会将int类型的直接转换成double类型的数据，这个问题还没有解决
		 */
		// 没有测试，深度需要多少？？？
		String jsonData = cmiPsonService.toJsonString(entitySet, 1);
		JSONArray jsonArray = new JSONArray();
		jsonArray = (JSONArray) JSON.parse((String) jsonData);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("list", jsonArray);
		jsonObject.put("paginationSize", paginationSize);
		jsonObject.put("pageNumber", pageNumber);
		jsonObject.put("totalRow", count);
		jsonObject.put("totalPaginations",
				count % paginationSize == 0 ? count / paginationSize : count / paginationSize + 1);
		return jsonObject;
	}
}
